﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace School
{
    public class Course
    {
        private const byte MaxStudentsInCourse = 29;
        private string courseName;
        private IList<Student> studentsInCourse = new List<Student>();

        public Course()
        {
        }

        public Course(string nameOfCourse ,List<Student> listOfStudents)
        {
            this.courseName = nameOfCourse;
            this.studentsInCourse = listOfStudents;
        }

        public List<Student> Students 
        {
            get
            {
                return this.studentsInCourse.ToList();
            }
            set
            {
                if (value.Count <= MaxStudentsInCourse)
                {
                    this.studentsInCourse = value;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("The count of the students in the course can not be more than " + MaxStudentsInCourse + "!");
                }
                
            }
        }

        public string NameOfCourse
        {
            get
            {
                return this.courseName;
            }
            set
            {
                if (value != null)
                {
                    this.NameOfCourse = value;
                }
                else
                {
                    throw new ArgumentNullException("The name of the course can not be null!");
                    
                }
                
            }
        }

        public void AddStudent(Student student)
        {
            if (!studentsInCourse.Contains(student) && studentsInCourse.Count < MaxStudentsInCourse)
            {
                studentsInCourse.Add(student);
            }
            else if (studentsInCourse.Contains(student))
            {
                throw new ArgumentException("The student " + student.Name + " is already recorded in " + this.courseName + " course!");
            }
            else if (studentsInCourse.Count == MaxStudentsInCourse)
            {
                throw new ArgumentException("The course is full! No more students can be added to this course!");
            }
        }

        public void RemoveStudent(Student student)
        {
            bool studentExists = CheckIfStudentExists(student);
            if (studentExists == true)
            {
                this.studentsInCourse.Remove(student);
            }
            else
            {
                throw new ArgumentException("The student " + student.Name + " is not recorded to this course!");
            }
        }

        private bool CheckIfStudentExists(Student student)
        {
            bool studentExists = false;
            for (int i = 0; i < this.studentsInCourse.Count; i++)
            {
                if (this.studentsInCourse[i].UniqueNumber == student.UniqueNumber)
                {
                    studentExists = true;
                }
            }

            return studentExists;
        }
    }
}
